/****************************************************************************
  From Qt Examples
****************************************************************************/

#ifndef PROXY_H
#define PROXY_H

#include <Qt>
#include <QStringList>
#include <QTcpServer>
#include <QTimer>
#include "serverload.h"

class Proxy : public QTcpServer
{
    Q_OBJECT

public:
    Proxy(QObject *parent = 0);
    ~Proxy();
    void setMsg(QString msg);
    //void sendMsg();
    void sendServerData(QString serverData);
    QString getCMSG();
   // void ProxyConnection(QString IP, int port);
   // void recieveProxyData();
    QList<ServerLoad*> *servers;
    //bool serverLoadLessThan(ServerLoad*, ServerLoad*);

signals:
    void transferMessage(QString);
    void sendInfo(QString);
    void timeSync(unsigned int syncTime);
    void serverAdded(ServerLoad*, void* threadID);
    void sendLoadToDialog(int,int);
    void loadBalanced(int,QHostAddress,quint16);
    void serverRemoved(ServerLoad*);
    void setAverageWorkload(int,int);

protected:
    void incomingConnection(int socketDescriptor);

public slots:
    //void ReadCMsg();
    void getMSG(QString);
    void addServer(int socketDescriptor, QString text, void* threadID);
    void removeServer(int socketDescriptor);
    void updateServer(int,int);
    void startSim();
    void stopSim();
    void progressSim();
    void balanceLoad(int);
    void updateList();
    void removeServerFromList(ServerLoad*);

private:
    QTimer *timer;
    QString message;
    QString proxyData;
    int SDescriptor;
    //QTcpSocket* connection;
    quint16 proxyBlockSize;
    QString currentMSG;
    QList<int> socketDescs;
    QTcpSocket *proxySocket;
    QList<int> serverSockets;
    QList<ServerLoad*> candidates;
    unsigned int simTime; //set from the gui
    double timeDenomination;    //set from the gui

    ServerLoad* findServer(int socketDescriptor);
};

#endif
